package so.shanku.essential.view;

/**
 * Created by Jaelyn on 2015/12/3 0003.
 */

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.KeyEvent;
import android.view.MotionEvent;

/**
 * 此ViewPager解决与父容器ScrollView冲突的问题,无法完美解决.有卡顿
 * 此自定义组件和下拉刷新scrollview配合暂时小完美，有待改善
 *
 * @author bavariama
 */

public class InsideViewPager extends ViewPager {

	public InsideViewPager(Context context) {
		super(context);
		//mGestureDetector = new GestureDetector(new YScrollDetector());
	}

	public InsideViewPager(Context context, AttributeSet attrs) {
		super(context, attrs);
		//mGestureDetector = new GestureDetector(new YScrollDetector());
	}

	@Override
	protected void onPageScrolled(int position, float offset, int offsetPixels) {
//		int kEvent;
//		if (isScrollingLeft(e1, e2)) { // Check if scrolling left
//			kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
//		} else { // Otherwise scrolling right
//			kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
//		}
//		if (1 == length) {
//			return true;
//		}
//		onKeyDown(kEvent, null);
//		if (this.getSelectedItemPosition() == 0)
//			this.setSelection(length);
		super.onPageScrolled(position, offset, offsetPixels);

	}


	private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) {
		return e2.getX() > e1.getX();
	}

	//	@Override
//	public boolean onTouchEvent(MotionEvent ev) {
////		curX = ev.getX();
////
////		if(mGestureDetector.onTouchEvent(ev)){
////			getParent().requestDisallowInterceptTouchEvent(true);
////		} else {
////			getParent().requestDisallowInterceptTouchEvent(false);
////		}
//	   return super.onTouchEvent(ev)        ;
//	}

	// Return false if we're scrolling in the x direction
	class YScrollDetector extends GestureDetector.SimpleOnGestureListener {
		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2,
		                        float distanceX, float distanceY) {
            return Math.abs(Math.abs(distanceY) - Math.abs(distanceX)) > 20;
        }
	}
	private GestureDetector mGestureDetector;


}

